package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.common.util.H5DomainUrlUtils;
import com.ts.api.http.platform.entity.apply.ApiPlatApplyHunXinQianBaoV2AnXin;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeHunXinQianBaoV2AnXin;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;
import com.ts.api.module.api.entity.ApiProtocol;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 安心分期-V2&绘信钱包
 *
 * 对接文档：https://apifox.com/apidoc/shared-1af02dd8-5bc1-4652-abee-5e56399b7cc4  api采量 YGFvRj7C
 *
 * @author WJB
 */
@ApiPlan(productId = "4", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatHunXinQianBaoV2AnXinPhoneMd5Convert implements ApiPlatformPhoneMd5ConvertHandler {

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        ApiPlatDataExchangeHunXinQianBaoV2AnXin orgDataExchangeYunXinQianBao = new ApiPlatDataExchangeHunXinQianBaoV2AnXin();
        orgDataExchangeYunXinQianBao.setPhoneMd5(apiDataExchangeParameter.getPhoneMd5());
        //  城市
        orgDataExchangeYunXinQianBao.setCity(apiDataExchangeParameter.getCity());
        if (ValidParam.isNotEmpty(apiDataExchangeParameter.getIp())) {
            orgDataExchangeYunXinQianBao.setUserIp(apiDataExchangeParameter.getIp());
        } else {
            orgDataExchangeYunXinQianBao.setUserIp("127.0.0.1");
        }
        orgDataExchangeYunXinQianBao.setName("浩安星");
        //  性别
        String sex = apiDataExchangeParameter.getSex();
        Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 0)
                .end(1);
        orgDataExchangeYunXinQianBao.setSex(sexChoose);
        //  年龄
        orgDataExchangeYunXinQianBao.setAge(ClassConvertUtils.toInt(apiDataExchangeParameter.getAge()));
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiDataExchangeParameter.getMonthlySalary(), "1", 1000)
                .when(apiDataExchangeParameter.getMonthlySalary(), "2", 3000)
                .when(apiDataExchangeParameter.getMonthlySalary(), "3", 8000)
                .when(apiDataExchangeParameter.getMonthlySalary(), "4", 15000)
                .when(apiDataExchangeParameter.getMonthlySalary(), "5", 30000)
                .end(1000);
        orgDataExchangeYunXinQianBao.setIncomeMonth(monthlySalary);
        //  贷款金额
        String loanAmount = apiDataExchangeParameter.getLoanAmount();
        Integer money = ChooseEq.create()
                .when(loanAmount, "1", 3)
                .when(loanAmount, "2", 5)
                .when(loanAmount, "3", 10)
                .when(loanAmount, "4", 20)
                .end(3);
        orgDataExchangeYunXinQianBao.setLoanAmount(money);
        //  借款期限
        orgDataExchangeYunXinQianBao.setLoanTerm(1);
        //  职业
        Integer profession = ChooseEq.create()
                .when(apiDataExchangeParameter.getCareer(), "1", 1)
                .when(apiDataExchangeParameter.getCareer(), "2", 2)
                .when(apiDataExchangeParameter.getCareer(), "3", 3)
                .when(apiDataExchangeParameter.getCareer(), "4", 5)
                .when(apiDataExchangeParameter.getCareer(), "5", 4)
                .end(6);
        orgDataExchangeYunXinQianBao.setProfession(profession);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiDataExchangeParameter.getProvidentFund(), "1", 1)
                .when(apiDataExchangeParameter.getProvidentFund(), "2", 2)
                .when(apiDataExchangeParameter.getProvidentFund(), "3", 3)
                .when(apiDataExchangeParameter.getProvidentFund(), "4", 2)
                .end(1);
        orgDataExchangeYunXinQianBao.setGjj(providentFund);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiDataExchangeParameter.getSocialSecurity(), "1", 1)
                .when(apiDataExchangeParameter.getSocialSecurity(), "2", 2)
                .when(apiDataExchangeParameter.getSocialSecurity(), "3", 3)
                .when(apiDataExchangeParameter.getSocialSecurity(), "4", 2)
                .end(1);
        orgDataExchangeYunXinQianBao.setSocialInsurance(socialSecurity);
        //  房产
        Integer realEstateInfo = ChooseEq.create()
                .when(apiDataExchangeParameter.getRealEstateInfo(), "1", 1)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "2", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "3", 3)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "4", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "5", 2)
                .when(apiDataExchangeParameter.getRealEstateInfo(), "6", 3)
                .end(1);
        orgDataExchangeYunXinQianBao.setHouse(realEstateInfo);
        //  车产
        Integer carInfo = ChooseEq.create()
                .when(apiDataExchangeParameter.getCarInfo(), "1", 1)
                .when(apiDataExchangeParameter.getCarInfo(), "2", 2)
                .when(apiDataExchangeParameter.getCarInfo(), "3", 3)
                .when(apiDataExchangeParameter.getCarInfo(), "4", 2)
                .end(1);
        orgDataExchangeYunXinQianBao.setCar(carInfo);
        //  芝麻分
        String sesameCredit = apiDataExchangeParameter.getSesameCredit();
        Integer zmf = ChooseEq.create()
                .when(sesameCredit, "1", 2)
                .when(sesameCredit, "2", 3)
                .when(sesameCredit, "3", 4)
                .when(sesameCredit, "4", 5)
                .end(1);
        orgDataExchangeYunXinQianBao.setSesame(zmf);
        //  商业保单
        Integer insurancePolicy = ChooseEq.create()
                .when(apiDataExchangeParameter.getInsurancePolicy(), "1", 1)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "2", 2)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "3", 3)
                .when(apiDataExchangeParameter.getInsurancePolicy(), "4", 2)
                .end(1);
        orgDataExchangeYunXinQianBao.setInsurancePolicy(insurancePolicy);
        // 代发
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiDataExchangeParameter.getSalaryPayMethod(), "1", 1)
                .when(apiDataExchangeParameter.getSalaryPayMethod(), "2", 2)
                .when(apiDataExchangeParameter.getSalaryPayMethod(), "3", 3)
                .end(1);
        orgDataExchangeYunXinQianBao.setPayType(salaryPayMethod);
        // 工龄
        Integer yearsService = ChooseEq.create()
                .when(apiDataExchangeParameter.getYearsService(), "1", 1)
                .when(apiDataExchangeParameter.getYearsService(), "2", 2)
                .when(apiDataExchangeParameter.getYearsService(), "3", 3)
                .end(1);
        orgDataExchangeYunXinQianBao.setWorkAge(yearsService);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiDataExchangeParameter.getBtLimit(), "1", 1)
                .when(apiDataExchangeParameter.getBtLimit(), "2", 2)
                .when(apiDataExchangeParameter.getBtLimit(), "3", 3)
                .when(apiDataExchangeParameter.getBtLimit(), "4", 4)
                .when(apiDataExchangeParameter.getBtLimit(), "5", 4)
                .end(1);
        orgDataExchangeYunXinQianBao.setWhiteNote(btLimit);
        //  学历
        Integer qualification = ChooseEq.create()
                .when(apiDataExchangeParameter.getEducationAttainment(), "1", 1)
                .when(apiDataExchangeParameter.getEducationAttainment(), "2", 2)
                .when(apiDataExchangeParameter.getEducationAttainment(), "3", 2)
                .when(apiDataExchangeParameter.getEducationAttainment(), "4", 3)
                .when(apiDataExchangeParameter.getEducationAttainment(), "5", 4)
                .when(apiDataExchangeParameter.getEducationAttainment(), "6", 5)
                .end(1);
        orgDataExchangeYunXinQianBao.setEducation(qualification);
        //  花呗
        Integer hbLimit = ChooseEq.create()
                .when(apiDataExchangeParameter.getHbLimit(), "1", 1)
                .when(apiDataExchangeParameter.getHbLimit(), "2", 2)
                .when(apiDataExchangeParameter.getHbLimit(), "3", 3)
                .when(apiDataExchangeParameter.getHbLimit(), "4", 4)
                .when(apiDataExchangeParameter.getHbLimit(), "5", 4)
                .end(1);
        orgDataExchangeYunXinQianBao.setFlowers(hbLimit);
        //  人行征信情况
        Integer overdue = ChooseEq.create()
                .when(apiDataExchangeParameter.getOverdue(), "1", 2)
                .when(apiDataExchangeParameter.getOverdue(), "2", 3)
                .when(apiDataExchangeParameter.getOverdue(), "3", 4)
                .end(1);
        orgDataExchangeYunXinQianBao.setCreditInformation(overdue);
        orgDataExchangeYunXinQianBao.setChannelOrderNo(apiDataExchangeParameter.getReqId());

        return JsonUtils.toJson(orgDataExchangeYunXinQianBao);
    }


    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        ApiPlatApplyHunXinQianBaoV2AnXin apply = new ApiPlatApplyHunXinQianBaoV2AnXin();
        apply.setUserPhone(apiApplyParameter.getPhone());
        //  城市
        apply.setCity(apiApplyParameter.getCity());
        if (ValidParam.isNotEmpty(apiApplyParameter.getIp())) {
            apply.setUserIp(apiApplyParameter.getIp());
        } else {
            apply.setUserIp("127.0.0.1");
        }
        apply.setName(apiApplyParameter.getUserName());
        //  性别
        String sex = apiApplyParameter.getSex();
        Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 0)
                .end(1);
        apply.setSex(sexChoose);
        //  年龄
        apply.setAge(ClassConvertUtils.toInt(apiApplyParameter.getAge()));
        //  月收入
        Integer monthlySalary = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 1000)
                .when(apiApplyParameter.getMonthlySalary(), "2", 3000)
                .when(apiApplyParameter.getMonthlySalary(), "3", 8000)
                .when(apiApplyParameter.getMonthlySalary(), "4", 15000)
                .when(apiApplyParameter.getMonthlySalary(), "5", 30000)
                .end(1000);
        apply.setIncomeMonth(monthlySalary);
        //  贷款金额
        String loanAmount = apiApplyParameter.getLoanAmount();
        Integer money = ChooseEq.create()
                .when(loanAmount, "1", 3)
                .when(loanAmount, "2", 5)
                .when(loanAmount, "3", 10)
                .when(loanAmount, "4", 20)
                .end(3);
        apply.setLoanAmount(money);
        //  借款期限
        apply.setLoanTerm(1);
        //  职业
        Integer profession = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 1)
                .when(apiApplyParameter.getCareer(), "2", 2)
                .when(apiApplyParameter.getCareer(), "3", 3)
                .when(apiApplyParameter.getCareer(), "4", 5)
                .when(apiApplyParameter.getCareer(), "5", 4)
                .end(6);
        apply.setProfession(profession);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 1)
                .when(apiApplyParameter.getProvidentFund(), "2", 2)
                .when(apiApplyParameter.getProvidentFund(), "3", 3)
                .when(apiApplyParameter.getProvidentFund(), "4", 2)
                .end(1);
        apply.setGjj(providentFund);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 1)
                .when(apiApplyParameter.getSocialSecurity(), "2", 2)
                .when(apiApplyParameter.getSocialSecurity(), "3", 3)
                .when(apiApplyParameter.getSocialSecurity(), "4", 2)
                .end(1);
        apply.setSocialInsurance(socialSecurity);
        //  房产
        Integer realEstateInfo = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 3)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 2)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 3)
                .end(1);
        apply.setHouse(realEstateInfo);
        //  车产
        Integer carInfo = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 1)
                .when(apiApplyParameter.getCarInfo(), "2", 2)
                .when(apiApplyParameter.getCarInfo(), "3", 3)
                .when(apiApplyParameter.getCarInfo(), "4", 2)
                .end(1);
        apply.setCar(carInfo);
        //  芝麻分
        String sesameCredit = apiApplyParameter.getSesameCredit();
        Integer zmf = ChooseEq.create()
                .when(sesameCredit, "1", 2)
                .when(sesameCredit, "2", 3)
                .when(sesameCredit, "3", 4)
                .when(sesameCredit, "4", 5)
                .end(1);
        apply.setSesame(zmf);
        //  商业保单
        Integer insurancePolicy = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 2)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 3)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 2)
                .end(1);
        apply.setInsurancePolicy(insurancePolicy);
        // 代发
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 2)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 3)
                .end(1);
        apply.setPayType(salaryPayMethod);
        // 工龄
        Integer yearsService = ChooseEq.create()
                .when(apiApplyParameter.getYearsService(), "1", 1)
                .when(apiApplyParameter.getYearsService(), "2", 2)
                .when(apiApplyParameter.getYearsService(), "3", 3)
                .end(1);
        apply.setWorkAge(yearsService);
        //  白条
        Integer btLimit = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", 1)
                .when(apiApplyParameter.getBtLimit(), "2", 2)
                .when(apiApplyParameter.getBtLimit(), "3", 3)
                .when(apiApplyParameter.getBtLimit(), "4", 4)
                .when(apiApplyParameter.getBtLimit(), "5", 4)
                .end(1);
        apply.setWhiteNote(btLimit);
        //  学历
        Integer qualification = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 2)
                .when(apiApplyParameter.getEducationAttainment(), "3", 2)
                .when(apiApplyParameter.getEducationAttainment(), "4", 3)
                .when(apiApplyParameter.getEducationAttainment(), "5", 4)
                .when(apiApplyParameter.getEducationAttainment(), "6", 5)
                .end(1);
        apply.setEducation(qualification);
        //  花呗
        Integer hbLimit = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", 1)
                .when(apiApplyParameter.getHbLimit(), "2", 2)
                .when(apiApplyParameter.getHbLimit(), "3", 3)
                .when(apiApplyParameter.getHbLimit(), "4", 4)
                .when(apiApplyParameter.getHbLimit(), "5", 4)
                .end(1);
        apply.setFlowers(hbLimit);
        //  人行征信情况
        Integer overdue = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 2)
                .when(apiApplyParameter.getOverdue(), "2", 3)
                .when(apiApplyParameter.getOverdue(), "3", 4)
                .end(1);
        apply.setCreditInformation(overdue);
        List<String>list = new ArrayList<>();
        list.add(H5DomainUrlUtils.createUserProtocolUrl(apiApplyParameter.getProductPlanId(), apiApplyParameter.getPhoneMd5()));
        apply.setProtocols(list);

        return JsonUtils.toJson(apply);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        return RespPlatformPhoneMd5Apply.ofSuc();

    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");

        JSONArray resProtocolList = resData.getJSONArray("agreements");

        BigDecimal price = resData.getBigDecimal("price");
        String companyName = resData.getString("companyName");
        String productName = resData.getString("productName");
        String logo = resData.getString("logo");
        String callUrl = resData.getString("url");
        List<ApiProtocol> protocolList = CollUtils.newArrayList(newList -> {
            if (ValidParam.isNotEmpty(resProtocolList)) {
                for (int i = 0; i < resProtocolList.size(); i++) {
                    JSONObject protocolObject = resProtocolList.getJSONObject(i);
                    ApiProtocol protocol = new ApiProtocol();
                    protocol.setProtocolName(protocolObject.getString("agreementName"));
                    protocol.setProtocolUrl(protocolObject.getString("agreementUrl"));
                    newList.add(protocol);
                }
            }
        });
        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();

        result.setPrice(price);
        result.setLogoPath(logo);
        result.setDisplayName(companyName);
        result.setProductName(productName);
        result.setCallUrl(callUrl);
        result.setProtocolList(protocolList);
        return result;
    }


    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        JSONObject jsonObject = JsonUtils.get(requestVO.getBody());
        String phoneMd5 = jsonObject.getString("phoneMd5");

        RespPlatformNoticeApply applyNotice = new RespPlatformNoticeApply();
        applyNotice.setPhoneMd5(phoneMd5);
        return applyNotice;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        boolean b = apiBizStatus.hasBizSuc();

        if (b) {
            return MapUtils.newHashMap(newHashMap -> {
                newHashMap.put("code", 0);
                newHashMap.put("message", "调用成功");
            });
        }
        return MapUtils.newHashMap(newHashMap -> {
            newHashMap.put("code", 500);
            newHashMap.put("message", "操作失败");
        });
    }

}
